import Vue from 'vue'
import VueRouter from 'vue-router'
// 将
// import UserDetails from './views/UserDetails.vue'
// 替换成
// const UserDetails = () => import('./views/UserDetails.vue')

// 导入token
import { getToken } from '@/utils/local'
// 一级路由
// import Register from '../views/Register.vue'
// import Login from '../views/Login.vue'
// import Detail from '../views/Detail.vue'
// import Layout from '../views/Layout.vue'

// // 二级路由
// import Article from '../views/Article.vue'
// import Collect from '../views/Collect.vue'
// import Like from '../views/Like.vue'
// import My from '../views/My.vue'

// 打包优化：路由懒加载
const Detail = () => import('@/views/Detail')
const Register = () => import('@/views/Register')
const Login = () => import('@/views/Login')
const Article = () => import('@/views/Article')
const Collect = () => import('@/views/Collect')
const Like = () => import('@/views/Like')
const My = () => import('@/views/My')
const Layout = () => import('@/views/Layout')
// const routes = [

// ]
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/register', component: Register },
    { path: '/login', component: Login },
    { path: '/detail/:id', component: Detail },
    {
      path: '/',
      component: Layout,
      redirect: '/article',
      children: [
        { path: '/article', component: Article },
        { path: '/collect', component: Collect },
        { path: '/like', component: Like },
        { path: '/my', component: My }
      ]
    }
  ]
})
const whidttoken = ['/login', '/register']
router.beforeEach((to, from, next) => {
  // ...
  const token = getToken()
  console.log(token)
  console.log(to, from)
  if (token) {
    next()
  } else {
    if (whidttoken.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }

  next()
})
export default router
